﻿using FRMS.DAL;
using FRMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.BUS
{
    public class RoomBUS
    {
        public ResponseMessage AddRoom(RoomInfo roomInfo)
        {
            ResponseMessage message = new ResponseMessage();
            RoomDAL da = new RoomDAL();
            string failinfo = string.Empty;
            string addinfo = string.Empty;
            string[] room_array = roomInfo.RoomName.Split(',');
            for (int i = 0; i < room_array.Length; i++)
            {
                int count = da.CheckRoom(roomInfo.BuildID, room_array[i], roomInfo.ID);
                if (count > 0)
                {
                    failinfo += room_array[i] + ",";
                }
                else
                {
                    roomInfo.RoomName = room_array[i];
                    bool result = da.AddRoom(roomInfo);
                    if (!result)
                    {
                        addinfo += room_array[i] + ",";
                    }
                }
            }
            if (failinfo.Length > 0)
            {
                message.errmsg += failinfo.Substring(0, failinfo.Length - 1) + "已存在，无需重复添加;";
            }
            if (addinfo.Length > 0)
            {
                message.errmsg += addinfo.Substring(0, addinfo.Length - 1) + "添加失败";
            }
            if (failinfo.Length > 0 || addinfo.Length > 0)
            {
                message.errcode = 1;
            }

            return message;
        }

        public ResponseMessage ImportRoom(RoomInfo roomInfo)
        {
            ResponseMessage message = new ResponseMessage();
            RoomDAL da = new RoomDAL();
            bool result = da.ImportRoom(roomInfo);
            if (result)
            {
                message.errcode = 0;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "房间信息导入失败";
            }

            return message;
        }

        public ResponseMessage EditRoom(RoomInfo roomInfo)
        {
            ResponseMessage message = new ResponseMessage();
            RoomDAL da = new RoomDAL();
            int count = da.CheckRoom(roomInfo.BuildID, roomInfo.RoomName, roomInfo.ID);
            if (count > 0)
            {
                message.errcode = 1;
                message.errmsg = "该房间已存在，请勿重复添加";
            }
            else
            {
                bool result = da.UpdateRoom(roomInfo);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改房间信息失败";
                }
            }

            return message;
        }

        public ResponseMessage DeleteRoom(RoomInfo roomInfo)
        {
            ResponseMessage message = new ResponseMessage();
            RoomDAL da = new RoomDAL();
            if (da.CheckOwner(roomInfo))
            {
                bool result = da.DelRoom(roomInfo);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "删除房间信息失败";
                }
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "该房间下绑定业主，请先删除业主信息";
            }


            return message;
        }

        public ResponseMessage QueryRoom(RoomInfo roomInfo)
        {
            ResponseMessage message = new ResponseMessage();
            RoomDAL da = new RoomDAL();
            message.errcode = 0;
            message.data = da.QueryRoomInfo(roomInfo);
            message.total = da.QueryRoomInfoCount(roomInfo);

            return message;
        }

        public ResponseMessage QueryRoomList(RoomInfo roomInfo)
        {
            ResponseMessage message = new ResponseMessage();
            RoomDAL da = new RoomDAL();
            message.errcode = 0;
            message.data = da.QueryRoomList(roomInfo);
            message.total = da.QueryRoomListCount(roomInfo);

            return message;
        }

        public ResponseMessage LoadRoom(RoomInfo roomInfo)
        {
            ResponseMessage message = new ResponseMessage();
            RoomDAL da = new RoomDAL();
            message.errcode = 0;
            message.data = da.LoadRoom(roomInfo.ID);

            return message;
        }
    }
}
